<body onload="animate()">
<!-- The image that will be animated. Give it a name for convenience. -->
<img name="animation" src="images/te0.jpg">

<script>
// Create a bunch of off-screen images, and pre-fetch the "frames"
// of the animation into them so that they're cached when we need them.
var aniframes = new Array(10);
for(var i = 0; i < 5; i++) {
    aniframes[i] = new Image();                 // Create an off-screen image
    aniframes[i].src = "images/te" + i + ".jpg";  // Tell it what URL to load.
}

var frame = 0;         // The frame counter: keeps track of current frame.
var timeout_id = null; // Allows us to stop the animation with clearTimeout()

// This function performs the animation.  Call it once to start.
// Note that we refer to the on-screen image using its name attribute.
function animate() {
    document.animation.src = aniframes[frame].src; // Display the current frame
    frame = (frame + 1)%4;                        // Update the frame counter
    timeout_id = setTimeout("animate()", 1000);     // Display next frame later.
}
</script>

<form>  <!-- This form contains buttons to control the animation. -->
  <input type="button" value="Start"
	 onclick="if (timeout_id == null) animate();">
  <input type="button" value="Stop" 
	 onclick="if (timeout_id) clearTimeout(timeout_id); timeout_id=null;">
</form>
</body>
